package com.wondertek.mist.plugins;

import org.apache.cordova.CordovaArgs;
import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import org.apache.cordova.api.PluginResult;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.wondertek.mist.NotifySignActivity;
import com.wondertek.mist.PolicySignActivity;

public class SignaturePlugin extends CordovaPlugin{

	public static final String MIST_NAME_EXTRA = "name";
	public static final String MIST_CARD_EXTRA = "card";
	public static final String MIST_SIGNTYPE_EXTRA = "signType";
	public static final String MIST_CALLBACK_EXTRA = "callback";
	public static final String MIST_BSDATA_EXTRA = "bsData";
	//单字签名入口
	private static final String ACTION_SINGLESIGN_EXTRA = "singleSign";
	//告知书签名入口
	private static final String ACTION_NOTIFYSIGN_EXTRA = "notifySign";

	private String callback;


	//	public static final String MIST_OKBUTTONTITLE_EXTRA = "okButtonTitle";



	@Override
	public boolean execute(String action, CordovaArgs args,
			CallbackContext callbackContext) throws JSONException {
		Log.i("execute()", "enter into plugin"); 


		if(ACTION_SINGLESIGN_EXTRA.equals(action)){
			//解析参数
			JSONObject Singlejson = (JSONObject) args.getJSONObject(0);


			Log.i("JSONObject", Singlejson.toString());

			initSingleSign(Singlejson);

			PluginResult result = new PluginResult(PluginResult.Status.NO_RESULT); 
			result.setKeepCallback(true); 
			callbackContext.sendPluginResult(result);

			Log.i("initWeb", "yes");

			return true;
		}else if (ACTION_NOTIFYSIGN_EXTRA.equals(action)) {
			JSONObject notifyjson = (JSONObject) args.getJSONObject(0);


			initNotifySign(notifyjson);

			PluginResult result = new PluginResult(PluginResult.Status.NO_RESULT); 
			result.setKeepCallback(true); 
			callbackContext.sendPluginResult(result);

			Log.i("initWeb", "yes");

			return true;

		}

		return false;

	}





	private void initNotifySign(JSONObject notifyjson) {
		// TODO Auto-generated method stub
		try {

			Intent intent = new Intent(this.cordova.getActivity(), NotifySignActivity.class);
			callback = notifyjson.getString("callback");
			intent.putExtra(MIST_NAME_EXTRA, notifyjson.getString("name"))
			.putExtra(MIST_CARD_EXTRA, notifyjson.getString("card"))
			.putExtra(MIST_BSDATA_EXTRA, notifyjson.getString("bsData"))
			.putExtra("tid", notifyjson.getString("tid"))
			.putExtra(MIST_CALLBACK_EXTRA, callback);

			this.cordova.startActivityForResult(SignaturePlugin.this, intent, 2);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}






	private void initSingleSign(final JSONObject singlejson) {

		try {

			Intent intent = new Intent(this.cordova.getActivity(), PolicySignActivity.class);
			//获取相关参数
			callback = singlejson.getString("callback");
			intent.putExtra(MIST_NAME_EXTRA, singlejson.getString("name"))
			.putExtra(MIST_CARD_EXTRA, singlejson.getString("card"))
			.putExtra(MIST_BSDATA_EXTRA, singlejson.getString("bsData"))
			.putExtra(MIST_CALLBACK_EXTRA, callback);


			this.cordova.startActivityForResult(SignaturePlugin.this, intent, 1);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent intent) {
		super.onActivityResult(requestCode, resultCode, intent);

		switch(requestCode){
		case 1:

			if(intent!=null){
				Bundle bundle = intent.getExtras();
				String param = bundle.getString(PolicySignActivity.SIGNATURE_SIGN);
				String bit_sigle = bundle.getString(PolicySignActivity.BITMAP_TWENTY);
				String bit_multi = bundle.getString(PolicySignActivity.BITMAP_THIRTY);
				this.webView.loadUrl("javascript:"+callback+
						"('"+param+"','"+bit_multi+"','"+bit_sigle+"');");
			}



			break;

		case 2:
           if (intent!=null) {
        	   Bundle b = intent.getExtras();
   			String par = b.getString(NotifySignActivity.SIGNATURE_NOTIFY);
   			String bit = b.getString(NotifySignActivity.BITMAP_NOTIFY);

   			this.webView.loadUrl("javascript:"+callback+"('"+par+"','"+bit+"');");
		} 
			
			

			break;




		}

	}

}
